7. BuildHeap in Linear Time It is easy to see that this tree consists of (2 0 ) node at height h , 2 1 nodes at height h –1, 2 2 at h- 2 and, in general, 2 i nodes at h – i. http://ecomputernotes.com
8. Complete Binary Tree A B h : 2 0 nodes H D I E J K C L F M G N O h -1: 2 1 nodes h -2: 2 2 nodes h -3: 2 3 nodes http://ecomputernotes.com
9. BuildHeap in Linear Time The sum of the heights of all the nodes is then S = 2 i ( h – i ), for i = 0 to h-1 = h + 2(h-1) + 4(h-2) + 8(h-3)+ ….. + 2 h-1 (1) Multiplying by 2 gives the equation 2S = 2 h + 4(h-1) + 8(h-2) + 16(h-3)+ ….. + 2 h (2) Subtract the two equations to get S = - h + 2 + 4 + 8 + 16+ ….. + 2 h-1 +2 h = (2 h+1 – 1) - (h+1) Which proves the theorem. http://ecomputernotes.com
10. BuildHeap in Linear Time Since a complete binary tree has between 2 h and 2 h+1 nodes S = (2 h+1 – 1) - (h+1) N - log 2 (N+1) Clearly, as N gets larger, the log 2 ( N +1) term becomes insignificant and S becomes a function of N . http://ecomputernotes.com
11.
12. Height 1 Nodes Marking the left edges for height 1 nodes http://ecomputernotes.com
13. Height 2 Nodes Marking the first left edge and the subsequent right edge for height 2 nodes http://ecomputernotes.com
14. Height 3 Nodes Marking the first left edge and the subsequent two right edges for height 3 nodes http://ecomputernotes.com
15. Height 4 Nodes Marking the first left edge and the subsequent three right edges for height 4 nodes http://ecomputernotes.com